package com.hnevc.trafficdemotest1.activity;

import android.os.Bundle;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hnevc.trafficdemotest1.R;
import com.hnevc.trafficdemotest1.fragment.AccountManagerFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    TextView mTvTitle ;
    ListView mListView ;
    ImageView mIvHome;

    String[] menuNameArray;
    int[] menuIconArray;
    List<Map<String,Object>> menuList;

    SlidingPaneLayout mSlidingPaneLayout;
    boolean flag =false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initView();
    }

    private void initView() {
        //获取侧滑控件
        mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.spl_root);
        //获取home按键
        mIvHome = (ImageView) findViewById(R.id.header_back_iv);
        //标题部分
        mTvTitle = (TextView) findViewById(R.id.header_title_tv);

        mListView = (ListView) findViewById(R.id.id_lv_menu);
        MyBaseMenuAdapter menuAdapter = new MyBaseMenuAdapter();
        mListView.setAdapter(menuAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mTvTitle.setText((String) menuList.get(i).get("name"));
                switch (i){
                    case 0://账户管理
                        Toast.makeText(MainActivity.this, "账户管理", Toast.LENGTH_SHORT).show();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,new AccountManagerFragment())
                                .commit();
                        break;
                    case 1:
                        //公交查询



                        break;
                    case 2:
                        //红绿灯管理


                        break;
                }
            }
        });
        //显示和掩藏侧滑菜单
        mIvHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(flag){
                    mSlidingPaneLayout.closePane();
                    flag =false;
                }else{
                    mSlidingPaneLayout.openPane();
                    flag = true;
                }
            }
        });
    }

    //初始化代码
    private void initData() {
        menuNameArray = getResources().getStringArray(R.array.menuarray);
        menuIconArray = new int[]{ R.drawable.a,R.drawable.b,R.drawable.c};
        menuList = new ArrayList<>();
        for(int i=0;i<menuNameArray.length;i++){
            Map map = new HashMap();
            map.put("name",menuNameArray[i]);
            map.put("icon",menuIconArray[i]);
            menuList.add(map);
        }
   }
   class MyBaseMenuAdapter extends BaseAdapter{
       @Override
       public int getCount() {
           return menuList.size();
       }

       @Override
       public Object getItem(int i) {
           return menuList.get(i);
       }

       @Override
       public long getItemId(int i) {
           return i;
       }

       @Override
       public View getView(int i, View view, ViewGroup viewGroup) {
           view = View.inflate(MainActivity.this, R.layout.menu_list_item, null);
           TextView mTvUserName = (TextView) view.findViewById(R.id.id_tv_menu_name);
           mTvUserName.setText((String) menuList.get(i).get("name"));
           ImageView mIvImageView = (ImageView) view.findViewById(R.id.id_iv_menu_icon);
           mIvImageView.setImageResource((Integer) menuList.get(i).get("icon"));
           return view;
       }
   }

}
